﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace IanUtil.DB
{
    public abstract class IU_DB_DataBase : IDisposable
    {
        protected object Connection { get; set; }

        private string connectionString;
        protected string ConnectionString { get { return connectionString; } }

        private DBType dbType;
        public DBType DBType { get { return dbType; } }

        /// <summary>
        /// 制造一个数据库实例
        /// </summary>
        /// <param name="connStr"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static IU_DB_DataBase Create(string connStr, DBType dbType)
        {
            IU_DB_DataBase db;
            switch (dbType)
            {
                case DBType.SQL_SERVER:
                    db = new IU_DB_SQLServer();
                    break;
                default:
                    db = new IU_DB_SQLServer();
                    break;
            }
            db.connectionString = connStr;
            db.dbType = dbType;
            return db;
        }

        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <returns></returns>
        public abstract object GetConnection();

        /// <summary>
        /// 返回SQL结果的第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public abstract object SQLForScalar(string sql);

        /// <summary>
        /// 返回SQL结果为DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public abstract DataSet SQLForDataSet(string sql);

        /// <summary>
        /// 返回SQL结果受影响的行数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public abstract int ExcuteSQL(string sql);

        /// <summary>
        /// 释放数据库连接
        /// </summary>
        public abstract void Dispose();
    }
}
